#include "Enemy.h"

#include "MobileObject.h"
#include "Player.h"
#include "..\Sprite Management\Sprite.h"
#include "..\\Message Management\\DeleteObjectMessage.h"
#include "..\..\SGD Wrappers\SGD_GraphicsManager.h"
#include "..\..\SGD Wrappers\SGD_Geometry.h"
#include "..\\AI States\\AIManagerState.h"
#include "..\\AI States\\AIState.h"

#include "..\Game.h"

Enemy::Enemy()
{
	SetAIState(GAME->GetAIManager()->GetAIState(AIState::State::PATROL));
	SetStateID(AIState::State::PATROL);
	SetAttackTimer(0.0f);
	SetIsKilled(false);

	// Set Initial moving direction
	IsMovingLeft(true);
	IsFacingLeft(true);

	// Set Default Prev Direction to true for left
	PrevDirection(true);
}


Enemy::~Enemy()
{
}

// Accessors
SGD::Point Enemy::GetPatrolPointLeft()
{
	return patrolPointLeft;
}

SGD::Point Enemy::GetPatrolPointRight()
{
	return patrolPointRight;
}

int Enemy::GetStateID()
{
	return iStateID;
}

// Mutators
AIState* Enemy::GetAIState()
{
	return pAIState;
}

void Enemy::SetAIState(AIState* state)
{
	pAIState = state;
}

void Enemy::SetPatrolPointLeft(SGD::Point point)
{
	patrolPointLeft = point;
}

void Enemy::SetPatrolPointRight(SGD::Point point)
{
	patrolPointRight = point;
}

void Enemy::SetStateID(int id)
{
	iStateID = id;
}

int Enemy::Update(float dt)
{
	if ((GetCurrentHP() <= 0 && !IsOnCamera() && GetID() != QUDZU && GetID() != PAPABEAR && GetID() != LOCKJAW) || GetAnimation()->GetScale() <= 0.0f) // Delete if offscreen and HP <= 0
	{
		DeleteObjectMessage* del = new DeleteObjectMessage(this);
		del->QueueMessage();
		del = nullptr;
		return 0;
	}
	else if (GetCurrentHP() <= 0) // Being knocked back
	{
		GetEmitters()[0]->SetPlay(true);
		GetEmitters()[0]->SetRepeat(true);

		GetAnimation()->SetScale(GetAnimation()->GetScale() - (dt * 1.25f));
		SetAttackRect(SGD::Rectangle(-1, -1, -1, -1));

		//////////////////////////////////////////////////////
		// Found Bug Fix ID: 11
		// Implemented by: Cliff Baird
		// Move the emitter up by 32 pixels
		//////////////////////////////////////////////////////
		SGD::Point emit = GetPosition();
		emit.y -= 32.0f;
		if (GetEmitters().size() > 0)
			GetEmitters()[0]->SetPosition(emit);
		//////////////////////////////////////////////////////
		// End Found Bug Fix Id: 11
		//////////////////////////////////////////////////////
		MobileObject::Update(dt);

		return 0;
	}

	// Prevent updates on enemies who are off screen
	if (!IsOnCamera())
	{
		SetAttackRect(SGD::Rectangle(-1, -1, -1, -1));
		return 0;
	}

	// If Player in Range, switch to attack
	if (!GetIsKilled())
	{
		// Player within range to attack for Ranged
		Player* player = GAME->GetPlayer();

		// Within range for Ranged enemy
		if (GetID() == RANGED && GetPosition().x >= player->GetPosition().x - 400
			&& GetPosition().x <= player->GetPosition().x + 400)
		{
			// Make Ranged Enemy move away from player a little
			if (player->IsMovingLeft())
			{
				IsFacingLeft(false); // Face player
				SetVelocity(SGD::Vector(-10.0f, GetVelocity().y));
				SetAcceleration(SGD::Vector(-10.0f, GetAcceleration().y));
			}
			else
			{
				IsFacingLeft(true); // Face player
				SetVelocity(SGD::Vector(10.0f, GetVelocity().y));
				SetAcceleration(SGD::Vector(10.0f, GetAcceleration().y));
			}

			// Set AI State To Ranged
			SetAIState(GAME->GetAIManager()->GetAIState(AIState::State::RANGE));
			SetStateID(AIState::State::RANGE);
		}
		// Within range for attack for Melee and Boss
		else if (GetPosition().x >= player->GetPosition().x - 100
			&& GetPosition().x <= player->GetPosition().x + 100)
		{
			// Flip enemy to face player
			if (GetPosition().x > player->GetPosition().x)
			{
				IsFacingLeft(true);
				IsMovingLeft(true);
			}
			else if (GetPosition().x < player->GetPosition().x)
			{
				IsFacingLeft(false);
				IsMovingLeft(false);
			}

			// Set AI State to MELEE
			SetAIState(GAME->GetAIManager()->GetAIState(AIState::State::MELEE));
			SetStateID(AIState::State::MELEE);
		}
		// Not Within Range
		else
		{
			SetAttackTimer(0.0f);
			SetAttackRect(SGD::Rectangle(-1, -1, -1, -1));

			// Set Boss To Follow Player
			if (GetID() == QUDZU || GetID() == LOCKJAW || GetID() == PAPABEAR)
			{
				SetAIState(GAME->GetAIManager()->GetAIState(AIState::State::FOLLOW));
				SetStateID(AIState::State::FOLLOW);
			}
			// Set Melee To Patrol AI State
			else if (GetID() == MELEE)
			{
				SetAIState(GAME->GetAIManager()->GetAIState(AIState::State::PATROL));
				SetStateID(AIState::State::PATROL);
			}

			if (IsMovingLeft())
			{
				if (GetID() == QUDZU || GetID() == PAPABEAR || GetID() == LOCKJAW)
				{
					// Set Acceleration to go left
					SetAcceleration(SGD::Vector{ -100, GetAcceleration().y });

					if (GetID() == QUDZU)
					{
						if (GetCurrentAnimationName() != "QudzuWalkLeft")
							SetAnimation("QudzuWalkLeft");
					}
					else if (GetID() == PAPABEAR)
					{
						if (GetCurrentAnimationName() != "PapaBearIdleLeft")
							SetAnimation("PapaBearIdleLeft");
					}
					else if (GetID() == LOCKJAW)
					{
						if (GetCurrentAnimationName() != "LockJawWalkingLeft")
							SetAnimation("LockJawWalkingLeft");
					}
				}
				else
				{
					// Set Acceleration to go left
					SetAcceleration(SGD::Vector{ -30, GetAcceleration().y });

					if (GetCurrentAnimationName() != "EnemyWalkingLeft")
						SetAnimation("EnemyWalkingLeft");
				}
			}
			else if (!IsMovingLeft())
			{
				if (GetID() == QUDZU || GetID() == PAPABEAR || GetID() == LOCKJAW)
				{
					// Set Moving Right
					SetAcceleration(SGD::Vector{ 100, GetAcceleration().y });

					if (GetID() == QUDZU)
					{
						if (GetCurrentAnimationName() != "QudzuWalkRight")
							SetAnimation("QudzuWalkRight");
					}
					if (GetID() == PAPABEAR)
					{
						if (GetCurrentAnimationName() != "PapaBearIdleRight")
							SetAnimation("PapaBearIdleRight");
					}
					else if (GetID() == LOCKJAW)
					{
						if (GetCurrentAnimationName() != "LockJawWalkingRight")
							SetAnimation("LockJawWalkingRight");
					}
				}
				else
				{
					// Set Moving Right
					SetAcceleration(SGD::Vector{ 30, GetAcceleration().y });

					if (GetCurrentAnimationName() != "EnemyWalkingRight")
						SetAnimation("EnemyWalkingRight");
				}
			}
		}

		// Run AI State's Update
		GetAIState()->Update(this, dt);

		//////////////////////////////////////////////////////
		// Found Bug Fix ID: 11
		// Implemented by: Cliff Baird
		// Move the emitter up by 32 pixels
		//////////////////////////////////////////////////////
		SGD::Point emit = GetPosition();
		emit.y -= 32.0f;
		if (GetEmitters().size() > 0)
			GetEmitters()[0]->SetPosition(emit);
		//////////////////////////////////////////////////////
		// End Found Bug Fix Id: 11
		//////////////////////////////////////////////////////

		// Run Entity's Update
		Entity::Update(dt);
	}
	return 0;
}

void Enemy::HandleCollision(const GameObject* collision)
{
	// Prevent HandlingCollision on enemies who are off screen
	if (!IsOnCamera())
		return;

	if (GetCurrentHP() <= 0)
		return;
}

void Enemy::HandleAttackCollision(const GameObject* collision)
{
	if (!IsInvulnerable())
	{
		GetEmitters()[0]->SetPlay(true);
		SetParticleTimer(0.75f);
		Entity::HandleAttackCollision(collision);
	}
}